﻿using Klear.Library.Services;
using OxyPlot;
using OxyPlot.Legends;
using OxyPlot.Axes;
using OxyPlot.Series;
using System.Globalization;


namespace Klear.Library.ViewModels;

/// <summary>
/// Represents the view-model for the main window.
/// </summary>
public class VisualizationViewModel : ViewModelBase
{
//OxyplotViewModel(要用的)-----------------------------------------------------------------------------------------------        
    public PlotModel PlotModelLeft { get; private set; }
    public PlotModel PlotModelRight { get; private set; }
    private readonly IVisualizationService _visualizationService;
    
    public VisualizationViewModel(IVisualizationService visualizationService)
    {
        _visualizationService = visualizationService;
        PlotModelLeft = new PlotModel { Title = "收入和支出情况" , Subtitle = "右键滑动以查看图表"};
        PlotModelRight = new PlotModel { Title = "每月预算和支出" , Subtitle = "右键滑动以查看图表"};
        InitializePlot();
    }
    
    private async Task InitializePlot()
    {
        var dtAxisLeft = new DateTimeAxis
        {
            Position = AxisPosition.Bottom,
            AbsoluteMinimum = DateTimeAxis.ToDouble(new DateTime(2024, 1, 1)),
            Maximum = DateTimeAxis.ToDouble(new DateTime(2024, 12, 31)),
            StringFormat = "M"
        };
        PlotModelLeft.Axes.Add(dtAxisLeft);
                
        var dtAxisRight = new DateTimeAxis
        {
            Position = AxisPosition.Bottom,
            AbsoluteMinimum = DateTimeAxis.ToDouble(new DateTime(2024, 1, 1)),
            Maximum = DateTimeAxis.ToDouble(new DateTime(2024, 12, 31)),
            StringFormat = "M"
        };
        PlotModelRight.Axes.Add(dtAxisRight);
    
        //坐标
        var valueAxisLeft = new LinearAxis
        {
            Position = AxisPosition.Left, 
            AbsoluteMinimum = 0,
            Maximum = 2000,
            MajorStep = 500,
            MinorStep = 5
        };
        PlotModelLeft.Axes.Add(valueAxisLeft);
        
        LinearAxis valueAxisRight = new LinearAxis
        {
            Position = AxisPosition.Left,
            AbsoluteMinimum = 0,
            Maximum = 5000,
            MajorStep = 500,
            MinorStep = 5
        };
        PlotModelRight.Axes.Add(valueAxisRight);
    
        //收入曲线
        var totalIncomeSeries = new LineSeries
        {
            Title = "收入",
            MarkerType = MarkerType.Circle,
            MarkerSize = 3
        };
        
        //支出曲线
        var leftTotalExpenseSeries = new LineSeries
        {
            Title = "支出",
            MarkerType = MarkerType.Square,
            MarkerSize = 3
        };
        
        //曲线下标
        Legend legendLeft = new Legend();
        legendLeft.LegendPosition = LegendPosition.BottomCenter;
        legendLeft.LegendPlacement = LegendPlacement.Outside;
        legendLeft.LegendOrientation = LegendOrientation.Horizontal;
        PlotModelLeft.Legends.Add((LegendBase) legendLeft);
        
        //预算曲线
        var budgetSeries = new LineSeries
        {
            Title = "预算",
            MarkerType = MarkerType.Circle,
            BrokenLineStyle = LineStyle.Dot,
            BrokenLineColor = OxyColors.Automatic,
            BrokenLineThickness = 1,
            MarkerSize = 3
        };
        
        //右边支出曲线
        var rightTotalExpenseSeries = new LineSeries
        {
            Title = "支出",
            MarkerType = MarkerType.Square,
            MarkerSize = 3
        };
        
        //曲线下标
        Legend legendRight = new Legend();
        legendRight.LegendPosition = LegendPosition.BottomCenter;
        legendRight.LegendPlacement = LegendPlacement.Outside;
        legendRight.LegendOrientation = LegendOrientation.Horizontal;
        PlotModelRight.Legends.Add((LegendBase) legendRight);
        
        var model = await _visualizationService.GetVisualizationDataAsync();
        var totalIncomeSeriesValue = new List<float>();
        var leftTotalExpenseSeriesValue = new List<float>();
        var budgetSeriesValue = new List<float>();
        var rightTotalExpenseSeriesValue = new List<float>();
        
        for (int i = 0; i < 12; i++)
        {
            DateTime date = new DateTime(2024, i + 1, DateTime.DaysInMonth(2024, i+1));
                
            // 使用从服务中获取的数据填充数组
            totalIncomeSeriesValue.Add(model.totalIncome[i]);
            leftTotalExpenseSeriesValue.Add(model.totalExpense[i]);
            rightTotalExpenseSeriesValue.Add(model.totalExpense[i]);
            //预算部分分为有预算和无预算两种情况
            
            totalIncomeSeries.Points.Add(new DataPoint(DateTimeAxis.ToDouble(date), totalIncomeSeriesValue[i]));
            leftTotalExpenseSeries.Points.Add(new DataPoint(DateTimeAxis.ToDouble(date), leftTotalExpenseSeriesValue[i]));
            rightTotalExpenseSeries.Points.Add(new DataPoint(DateTimeAxis.ToDouble(date), rightTotalExpenseSeriesValue[i]));
            
            if (i+1 > model.budget.Count)
            {
                budgetSeriesValue.Add(0);
                budgetSeries.Points.Add(new DataPoint(DateTimeAxis.ToDouble(date), budgetSeriesValue[i]));
            }
            else
            {
                budgetSeriesValue.Add((float)model.budget[i].Budget);
                budgetSeries.Points.Add(new DataPoint(DateTimeAxis.ToDouble(model.budget[i].EndDate), budgetSeriesValue[i]));
                budgetSeries.Points.Add(new DataPoint(DateTimeAxis.ToDouble(model.budget[i].StartDate), budgetSeriesValue[i]));
                budgetSeries.Points.Add(new DataPoint(DateTimeAxis.ToDouble(date), 0));
            }
            
        }
        
        PlotModelLeft.Series.Add(totalIncomeSeries);
        PlotModelLeft.Series.Add(leftTotalExpenseSeries);
        PlotModelRight.Series.Add(rightTotalExpenseSeries);
        PlotModelRight.Series.Add(budgetSeries);

        for (int i = 0; i < budgetSeries.Points.Count; i++)
        {
            if (budgetSeries.Points[i].Y == 0)
            {
                budgetSeries.Points[i] = DataPoint.Undefined;            
            }
        }
        
    }
        
}




